package cn.edu.zju.om.data;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Map.Entry;

import cn.edu.zju.om.nlp.SUWordSegmentorAdapter;
import cn.edu.zju.om.nlp.WordSegmentor;

public class DataAnalyser {

	
	static private Map<String, Integer> wordsCount = new HashMap<String, Integer>();
	
	static private LinkedList<Integer> record = new LinkedList<Integer>();
	
	static private Set<String> printedWords = new HashSet<String>();
	
	static private FileWriter writer;
	
	static final int MAX_CAPACITY = 100;
	
	static private void init() {
		for(int i = 0; i < MAX_CAPACITY - 1; ++i) {
			record.add(new Integer(1));
		}
		

	}
	
	static private void updateWordsCount(List<String> wordList) {
		for(String word : wordList) {
			if (word.trim().length() <= 1) {
				continue;
			}
			
			if (wordsCount.get(word) == null) {
				wordsCount.put(word, new Integer(1));
			}
			else {
				int newCount = wordsCount.get(word).intValue() + 1;
				Integer newCountObj = new Integer(newCount);
				wordsCount.put(word, newCountObj);
				
				if (newCount > record.get(0).intValue() && record.size() <= MAX_CAPACITY) {
					//record.remove(0);
					Integer lastValue = new Integer(newCount - 1);
					int lastValueIndex = record.lastIndexOf(lastValue);
					record.remove(lastValueIndex);
					record.add(newCountObj);
					Collections.sort(record);
				}
			}
		}
	}
	
	public static void main(String[] args) throws IOException {
		init();
		
		WordSegmentor segmentor = new SUWordSegmentorAdapter();
		
		DAO<ReviewTarget, String> reviewTargetsDAO = new DAO<ReviewTarget, String>(ReviewTarget.class);
		
		List<ReviewTarget> targets = reviewTargetsDAO.findAll();
		
		for(ReviewTarget target : targets) {
			wordsCount.clear();
			printedWords.clear();
			record.clear();
			
			init();
			
			writer = new FileWriter(target.getName() + ".txt");
			
			
			processTarget(segmentor, target);
			
			printResult();
			
			writer.close();
		}
		//ReviewTarget target = reviewTargetsDAO.findById("8acc80c4262bdcfc01262bdd0afe0001", false);
		

	}

	private static void printResult() throws IOException {
		for (Entry<String, Integer> entry : wordsCount.entrySet()) {
			for (Integer count : record) {
				if (entry.getValue().compareTo(count) == 0) {
					if (!printedWords.contains(entry.getKey())) {
						//System.out.println(String.format("%s : %d", entry.getKey(), entry.getValue().intValue()));
						writer.write(String.format("%s\t%d" + System.getProperty("line.separator"), entry.getKey(), entry.getValue().intValue()));
						printedWords.add(entry.getKey());
					}
					
				}
			}
		}
	}

	private static void processTarget(WordSegmentor segmentor,
			ReviewTarget target) {
		Set<Comment> comments = target.getComments();
		
		for (Comment comment : comments) {
			String reviewText = comment.getText();
			
			if (reviewText == null) {
				continue;
			}
			
			try {
				List<String> words = segmentor.segment(reviewText);
				
				updateWordsCount(words);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
